//
//  NSString+sizeCalculate.m
//  焰火化学
//
//  Created by mac-01 on 2017/3/29.
//  Copyright © 2017年 杭州秀铂科技网络有限公司. All rights reserved.
//

#import "NSString+sizeCalculate.h"

@implementation NSString (sizeCalculate)

/**
 计算字符串的大小
 
 @param size 限定最大大小
 @param font 字体
 @return 字符串大小
 */
- (CGSize)SizeCalculateWithSize:(CGSize)size font:(UIFont *)font {

    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    
}

/**
 设置行间距和字间距

 @param lineSpace 行间距
 @return 富文本
 */
-(NSAttributedString*)getAttributedStringWithLineSpace:(CGFloat)lineSpace {
    NSMutableParagraphStyle*paragraphStyle = [NSMutableParagraphStyle new];
    //调整行间距
    paragraphStyle.lineSpacing= lineSpace;
    NSDictionary*attriDict =@{NSParagraphStyleAttributeName:paragraphStyle};
    NSMutableAttributedString*attributedString = [[NSMutableAttributedString  alloc] initWithString:self attributes:attriDict];
    
    return attributedString;
}

- (CGSize)getAttributionHeightWithString:(NSString *)string lineSpace:(CGFloat)lineSpace font:(UIFont *)font width:(CGFloat)width {
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = lineSpace;
    
    NSDictionary *dict = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle};
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                    attributes:dict
                                       context:nil].size;
    return size;
    
}


@end
